<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/DTD/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
	<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
	<title>Pavco</title>
	
	<!-- Load the Google AJAX API Loader and set the key -->
	<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAporGMUZQY1wIQWgCBwmHahTnznCKwKvlBA0qLP7pqoQydu37nRR7FgBywU3xgJjWvob5YzYuF3QRVg"></script>
  <!-- Load the Google Friend Connect javascript library. -->
	<script type="text/javascript">
		google.load("maps", "2.x");
		google.load('friendconnect', '0.8');
  </script>
    
  <script type="text/javascript">
  	// CaribbeanVisit
	  //var SITE_ID = '04312132550820759615';
	  // CaribbeanVisitTest
	  var SITE_ID = '10066347073652436514';
	  // 1.latest.caribbeanvisit.appspot.com
	  //var SITE_ID = '01271000652931307915';
		google.friendconnect.container.setParentUrl('/');
		google.friendconnect.container.initOpenSocialApi({
			id: 'gfc-signin',
			site: SITE_ID, 
			onload: function(securityToken) { renderGfcSignin(); }
		});
  </script>
	<script type="text/javascript" language="javascript" src="caribbeanvisit/caribbeanvisit.nocache.js"></script>
</head>
<body onunload="GUnload()">
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
    <noscript>
      <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
        Your web browser must have JavaScript enabled
        in order for this application to display correctly.
      </div>
    </noscript>
<%
	// No need to get this async'ly
	String useragent = request.getHeader("user-agent");
	if (useragent.toLowerCase().contains("iphone")) {
	  response.sendRedirect("/m/");
	}
%>

<script type="text/javascript">
	function renderGfcSignin() {
		var req = opensocial.newDataRequest();
    var params = {};
    params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] =
        [opensocial.Person.Field.ID,
         opensocial.Person.Field.NAME,
         opensocial.Person.Field.THUMBNAIL_URL,
         opensocial.Person.Field.PROFILE_URL];
    var viewer_idspec = new opensocial.IdSpec({
      'userId' : 'VIEWER',
      'groupId' : 'FRIENDS'
    });
    var owner_idspec = new opensocial.IdSpec({
        'userId' : 'OWNER',
        'groupId' : 'FRIENDS'
    });
    req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer');
    req.add(req.newFetchPeopleRequest(viewer_idspec, params), 'viewer_friends');
    req.add(req.newFetchPeopleRequest(owner_idspec, params), 'site_friends');
    req.send(onData);
  }
  
  function onData(data) {
  	if (data.get('viewer').hadError()) {
      setSignedInWidgetText('', false);
      return;
    } else {
      var viewer = data.get('viewer').getData();
      setSignedInWidgetText(viewer.getDisplayName(), true);
    }

		if (!data.get("viewer_friends").hadError()) {
      var viewers_friends = data.get("viewer_friends").getData();
      var friends = new Array();
      var index = 0;
      viewers_friends.each(
        function(friend) {
          friends[index++] = {"friendId": friend.getId(),
                          "thumbnailUrl": friend.getField("thumbnailUrl"),
                          "profileUrl": friend.getField("profileUrl"),
                          "status": friend.getField("status"),
                          "displayName": friend.getDisplayName()
                          };
        }
      );
      addViewerFriends(friends);
    }

		if (!data.get("site_friends").hadError()) {
      var site_friends = data.get("site_friends").getData();
      var friends = new Array();
      var index = 0;
      site_friends.each(
        function(friend) {
          friends[index++] = {"friendId": friend.getId(),
                            "thumbnailUrl": friend.getField("thumbnailUrl"),
                            "profileUrl": friend.getField("profileUrl"),
                            "status": friend.getField("status"),
                            "displayName": friend.getDisplayName()
                            };
        }
      );
      addSiteFriends(friends);
    }
  }
  </script>
</body>
</html>